/**
 * 
 */
package it.unisalento.appsalento.asynctasc;

import it.unisalento.appsalento.Constants.Constants;
import it.unisalento.appsalento.activity.App;
import it.unisalento.appsalento.asynctask.framework.MIMETypes;
import it.unisalento.appsalento.bo.mock.MockBo;
import it.unisalento.appsalento.model.Appello;
import it.unisalento.appsalento.model.GetAppelliResponse;
import it.unisalento.appsalento.model.Request;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.content.Context;
import android.view.MenuItem;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
 * @author AGM
 * 
 */
public class CalendarAsyncTask extends SyncAsyncTask {

	public CalendarAsyncTask(Context context, MenuItem refreshMenuItem) {
		this.context = context;
		this.refreshMenuItem = refreshMenuItem;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (refreshMenuItem != null) {
			if (App.calendarioFragment != null
					&& App.calendarioFragment.isVisible()) {
				App.calendarioFragment.refreshCalendar();
			}
			refreshMenuItem.collapseActionView();
			// remove the progress bar view
			refreshMenuItem.setActionView(null);
		}
		if (App.homeFragment != null && App.homeFragment.isVisible()) {
			App.homeFragment.refreshRisultatiButton();
		}
//		progress.hide();

	}

	public boolean setData(ObjectMapper mapper) throws JSONException, UnsupportedEncodingException, IOException,
			ClientProtocolException, JsonParseException, JsonMappingException {
		boolean retVal = false;
		try {
			GetAppelliResponse appelliResponse = null;
			if(!Constants.MOCK_ENABLED){
				Request request = new Request();
				request.setUtente(App.utente);
				final Gson gson = new GsonBuilder().create();
				final Client client = Client.create();
			    final WebResource resource = client.resource(Constants.URL_REST + Constants.SERVICE_LISTA_APPELLI);
			    final ClientResponse response = resource.type(MIMETypes.APPLICATION_JSON.getName()).post(ClientResponse.class, gson.toJson(request));
			    if (response.getStatus() != 201 && response.getStatus() != 200) {
			      throw new RuntimeException("failed: http error code = " + response.getStatus());
			    }
			    final String responseEntity = response.getEntity(String.class).replaceAll("\\\\", "");
			    appelliResponse = mapper.readValue(responseEntity,GetAppelliResponse.class);
			}else{
				InputStream is = context.getAssets().open(MockBo.GET_APPELLI_RESPONSE);
				appelliResponse = mapper.readValue(is,GetAppelliResponse.class);
			}
			if(appelliResponse != null){
				App.appelli = new ArrayList<Appello>();
				if (appelliResponse.isOk()) {
					App.appelli = new ArrayList<Appello>();
					App.appelli.addAll(appelliResponse.getListaAppelli());
				}
			
				retVal = appelliResponse.isOk();
			}
		} catch (Throwable t) {
			App.appelli = new ArrayList<Appello>();
			t.printStackTrace();
		}
		return retVal;
	}
}
